<?php

/*============================================
Classe Iaantenna
============================================*/

class Iaantenna extends ModelObject{

	protected static $cached_object_list= array();
	protected $ANT_ID_EPHEMERIS;
	protected $DEVICE_NAME;
	protected $RESSOURCES;
	protected $MAX_DURATION;
	protected $ANT_PERIOD;
	protected $PERIOD;
	protected $NB_EXEC;
	protected $PRIORITY;

	protected function __construct( $ANT_ID_EPHEMERIS,$DEVICE_NAME,$RESSOURCES,$MAX_DURATION,$ANT_PERIOD,$PERIOD,$NB_EXEC,$PRIORITY){
		try{
			
			$this->ANT_ID_EPHEMERIS = $ANT_ID_EPHEMERIS;
			$this->DEVICE_NAME = $DEVICE_NAME;
			$this->RESSOURCES = $RESSOURCES;
			$this->MAX_DURATION = $MAX_DURATION;
			$this->ANT_PERIOD = $ANT_PERIOD;
			$this->PERIOD = $PERIOD;
			$this->NB_EXEC = $NB_EXEC;
			$this->PRIORITY = $PRIORITY;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getANTIDEPHEMERIS(){
		return $this->ANT_ID_EPHEMERIS;
	}

	public function getDEVICENAME(){
		return DEVICENAME::get($this->DEVICE_NAME);
	}

	public function getRESSOURCES(){
		return $this->RESSOURCES;
	}

	public function getMAXDURATION(){
		return $this->MAX_DURATION;
	}

	public function getANTPERIOD(){
		return $this->ANT_PERIOD;
	}

	public function getPERIOD(){
		return $this->PERIOD;
	}

	public function getNBEXEC(){
		return $this->NB_EXEC;
	}

	public function getPRIORITY(){
		return $this->PRIORITY;
	}

	public function setDEVICENAME( $DEVICE_NAME){
		try{
			$temp = $this->DEVICE_NAME;
			if(is_null($DEVICE_NAME)){
				throw new Exception("Le paramètre \$dEVICE_NAME de type get_class($dEVICE_NAME) est null alors que la colonne dans la base est marquée comme NOT NULL");
			}
			else if(!($DEVICE_NAME instanceof DEVICENAME) || is_null($DEVICE_NAME->getId())){
				throw new Exception("Le paramètre \$dEVICE_NAME de type get_class($dEVICE_NAME) n'est pas du bon type ou n'est pas présent dans la base");
			}
			else {
				if($this->DEVICE_NAME !== $DEVICE_NAME->getId()){
					$this->DEVICE_NAME = $DEVICE_NAME->getId();
					$this->update();
				}
			}
		}
		catch(Exception $e){
			$this->DEVICE_NAME = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDEVICENAME() a échouée",0,$e);
		}
	}

	public function setRESSOURCES( $RESSOURCES){
		try{
			$temp = $this->RESSOURCES;
			if($this->RESSOURCES !== $RESSOURCES){
				$this->RESSOURCES = $RESSOURCES;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->RESSOURCES = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setRESSOURCES() a échouée",0,$e);
		}
	}

	public function setMAXDURATION( $MAX_DURATION){
		try{
			$temp = $this->MAX_DURATION;
			if($this->MAX_DURATION !== $MAX_DURATION){
				$this->MAX_DURATION = $MAX_DURATION;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MAX_DURATION = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMAXDURATION() a échouée",0,$e);
		}
	}

	public function setANTPERIOD( $ANT_PERIOD){
		try{
			$temp = $this->ANT_PERIOD;
			if($this->ANT_PERIOD !== $ANT_PERIOD){
				$this->ANT_PERIOD = $ANT_PERIOD;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->ANT_PERIOD = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setANTPERIOD() a échouée",0,$e);
		}
	}

	public function setPERIOD( $PERIOD){
		try{
			$temp = $this->PERIOD;
			if($this->PERIOD !== $PERIOD){
				$this->PERIOD = $PERIOD;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->PERIOD = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setPERIOD() a échouée",0,$e);
		}
	}

	public function setNBEXEC( $NB_EXEC){
		try{
			$temp = $this->NB_EXEC;
			if($this->NB_EXEC !== $NB_EXEC){
				$this->NB_EXEC = $NB_EXEC;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->NB_EXEC = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setNBEXEC() a échouée",0,$e);
		}
	}

	public function setPRIORITY( $PRIORITY){
		try{
			$temp = $this->PRIORITY;
			if($this->PRIORITY !== $PRIORITY){
				$this->PRIORITY = $PRIORITY;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->PRIORITY = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setPRIORITY() a échouée",0,$e);
		}
	}

	public static function create( $ANT_ID_EPHEMERIS,$DEVICE_NAME,$RESSOURCES,$MAX_DURATION,$ANT_PERIOD,$PERIOD,$NB_EXEC,$PRIORITY){
		try{
			$iaantenna = new Iaantenna($ANT_ID_EPHEMERIS, $DEVICE_NAME, $RESSOURCES, $MAX_DURATION, $ANT_PERIOD, $PERIOD, $NB_EXEC, $PRIORITY);
			$iaantenna->insert();
			Iaantenna::cacheObject($iaantenna);
			return $iaantenna;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type Iaantenna a échouée",0,$e);
		}
	}

	public static function get( $ANT_ID_EPHEMERIS){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($ANT_ID_EPHEMERIS) || $ANT_ID_EPHEMERIS === "") {
				return null;
			}
			if(Iaantenna::existsInCache($ANT_ID_EPHEMERIS)) {
				return Iaantenna::getFromCache($ANT_ID_EPHEMERIS);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `iaantenna` WHERE `ANT_ID_EPHEMERIS` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$ANT_ID_EPHEMERIS));
			if ($row = $stmt->fetch()){
				$iaantenna = new Iaantenna( $row['ANT_ID_EPHEMERIS'],$row['DEVICE_NAME'],$row['RESSOURCES'],$row['MAX_DURATION'],$row['ANT_PERIOD'],$row['PERIOD'],$row['NB_EXEC'],$row['PRIORITY']);
				$iaantenna->id =  $row['ANT_ID_EPHEMERIS'];
				$dbh->commit();
				Iaantenna::cacheObject($iaantenna);
				return $iaantenna;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $ANT_ID_EPHEMERIS . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `iaantenna` SET `DEVICE_NAME` = :DEVICE_NAME, `RESSOURCES` = :RESSOURCES, `MAX_DURATION` = :MAX_DURATION, `ANT_PERIOD` = :ANT_PERIOD, `PERIOD` = :PERIOD, `NB_EXEC` = :NB_EXEC, `PRIORITY` = :PRIORITY WHERE `ANT_ID_EPHEMERIS` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':DEVICE_NAME'=>$this->DEVICE_NAME, ':RESSOURCES'=>$this->RESSOURCES, ':MAX_DURATION'=>$this->MAX_DURATION, ':ANT_PERIOD'=>$this->ANT_PERIOD, ':PERIOD'=>$this->PERIOD, ':NB_EXEC'=>$this->NB_EXEC, ':PRIORITY'=>$this->PRIORITY, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `iaantenna` WHERE `ANT_ID_EPHEMERIS` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			Iaantenna::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `iaantenna` ( `DEVICE_NAME`, `RESSOURCES`, `MAX_DURATION`, `ANT_PERIOD`, `PERIOD`, `NB_EXEC`, `PRIORITY`) VALUES (:DEVICE_NAME, :RESSOURCES, :MAX_DURATION, :ANT_PERIOD, :PERIOD, :NB_EXEC, :PRIORITY)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':DEVICE_NAME'=>$this->DEVICE_NAME, ':RESSOURCES'=>$this->RESSOURCES, ':MAX_DURATION'=>$this->MAX_DURATION, ':ANT_PERIOD'=>$this->ANT_PERIOD, ':PERIOD'=>$this->PERIOD, ':NB_EXEC'=>$this->NB_EXEC, ':PRIORITY'=>$this->PRIORITY));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `iaantenna`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(Iaantenna::existsInCache($row['ANT_ID_EPHEMERIS'])){
					$obj = Iaantenna::getFromCache($row['ANT_ID_EPHEMERIS']);
				}
				else{
					$obj = new Iaantenna( $row['ANT_ID_EPHEMERIS'],$row['DEVICE_NAME'],$row['RESSOURCES'],$row['MAX_DURATION'],$row['ANT_PERIOD'],$row['PERIOD'],$row['NB_EXEC'],$row['PRIORITY']);
				}
				if(!is_null($obj)){
					$obj->id = $row['ANT_ID_EPHEMERIS'];
					Iaantenna::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iaantenna a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `iaantenna`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iaantenna a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `iaantenna`';
			$sql_select = 'SELECT ANT_ID_EPHEMERIS FROM `iaantenna`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = Iaantenna::getObjectFromCache($row['ANT_ID_EPHEMERIS']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						Iaantenna::removeObjectFromCache($row['ANT_ID_EPHEMERIS']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type Iaantenna a échouée",0,$e);
		}
	}

}